package com.tul.exercise.base.swing.components;

import com.tul.exercise.base.swing.SwingConsole;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 《java编程思想》第22章 示例18：
 * <p>
 * 菜单
 **/
public class SimpleMenu extends JFrame {
    private JTextField textField = new JTextField(15);

    private ActionListener al = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            textField.setText(((JMenuItem) e.getSource()).getText());
        }
    };

    private List<JMenu> menus = Arrays.asList(
            new JMenu("Winken"), new JMenu("Blinken"), new JMenu("Nod")
    );

    private JMenuItem[] items = {
            new JMenuItem("Fee"), new JMenuItem("Fi"),
            new JMenuItem("Fo"), new JMenuItem("Zip"),
            new JMenuItem("Zap"), new JMenuItem("Zot"),
            new JMenuItem("Olly"), new JMenuItem("Oxen"),
            new JMenuItem("Free")
    };

    public SimpleMenu() {
        for (int i = 0; i < items.length; i++) {
            items[i].addActionListener(al);
            menus.get(i%3).add(items[i]);
        }

        JMenuBar mb = new JMenuBar();

        for(JMenu jm:menus) {
            mb.add(jm);
        }

        setJMenuBar(mb);
        setLayout(new FlowLayout());
        add(textField);

    }

    public static void main(String[] args) {
        SwingConsole.run(new SimpleMenu(), 200, 150);
    }
}
